<?php

namespace app\admin\controller;

use app\admin\controller\CommonController;
use think\Validate;
use think\Db;
use app\util\Utils;
use app\admin\model\Custom;

/*虚拟用户*/
class CustomlanController extends CommonController {
    /*
     * 虚拟用户列表
     * author xfwed 
     * date 2021.08.03
     */

    public function customList() {
        $name = input('param.name');
        $status = input('param.status');
        $perPage = input('param.perPage');
        $page = input('param.page');
        
        $perPage = isset($perPage) ? $perPage : 10;
        $page = isset($page) ? $page : 1;
        
        if ($status) {
            $where['status'] = $status;
        }
        if ($name) {
            $where['name'] = ['like','%'.$name.'%'];
        }
        
        $where = isset($where) ? $where : [];
        $customList = Db::name('custom_lan')
                ->where($where)
                ->paginate($perPage);

        return $this->ajaxReturn(0, '查询成功', $customList);
    }
    
    /*
     * 添加虚拟用户
     * author xfwed
     * date 2021.08.03
     */

    public function addCustomlan() {
        $rule = [
            'name' => 'require',
            'img' => 'require',
        ];
        $msg = [
            'name.require' => '昵称必须',
            'img.require' => '头像必须',
        ];
        $all = input('post.');
        $validate = new Validate($rule, $msg);

        if (!$validate->check($all)) {
            $error = $validate->getError();
            return $this->ajaxReturn(1, $error, null);
        }

        $data['name'] = $all['name'];
        $data['img'] = $all['img'];
        $data['status'] = 1;
        $data['add_time'] = date('Y-m-d H:i:s');
        $add = Db::name('custom_lan')->insert($data);
        if ($add) {
            return $this->ajaxReturn(0, '添加成功', null);
        } else {
            return $this->ajaxReturn(3, '添加失败', null);
        }
    }
    
    /*
     * 启用/禁用虚拟用户
     * author xfwed
     * date 2021.08.03
     */

    public function saveCustomlanStatus() {
        $rule = [
            'id' => 'require',
            'status' => 'require',
        ];
        $msg = [
            'id.require' => '编号必须',
            'status.require' => '状态必须',
        ];
        $all = input('post.');
        $validate = new Validate($rule, $msg);

        if (!$validate->check($all)) {
            $error = $validate->getError();
            return $this->ajaxReturn(1, $error, null);
        }

        $add = Db::name('custom_lan')
                ->where('id',$all['id'])
                ->update(['status'=>$all['status']]);
        if ($add) {
            return $this->ajaxReturn(0, '操作成功', null);
        } else {
            return $this->ajaxReturn(3, '操作失败', null);
        }
    }

   
}
